package server;

import helpers.SelfRemoting;

import java.rmi.RemoteException;
import java.util.concurrent.LinkedBlockingQueue;

import remote.PokerPlayer;
import remote.PokerRoom;

public class PokerRoomServant extends SelfRemoting<PokerRoom> implements
		PokerRoom {

	private final LinkedBlockingQueue<PokerPlayer> waitingList;

	public PokerRoomServant() throws RemoteException {
		waitingList = new LinkedBlockingQueue<PokerPlayer>();
		enpair();
	}

	@Override
	public void play(PokerPlayer pp) throws RemoteException {
		waitingList.add(pp);
	}

	public void enpair() {
		new Thread() {

			@Override
			public void run() {

				while (true) {
					final PokerArbiter pa;
					try {
						pa = new PokerArbiter(waitingList.take(),
								waitingList.take());
					} catch (Exception e) {
						return;
					}

					new Thread(pa).start();
				}
			}

		}.start();
	}
}
